package com.smart.service;

import com.smart.model.Customer;
import com.smart.util.PropsUtil;
import sun.print.DocumentPropertiesUI;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class CustomerService {

    private static final String driver;
    private static final String url;
    private static final String userName;
    private static final String passWord;

    static {
        Properties conf= PropsUtil.loadProps("config.properties");
        driver=conf.getProperty("jdbc.driver");
        url=conf.getProperty("jdbc.url");
        userName=conf.getProperty("jdbc.username");
        passWord=conf.getProperty("jdbc.password");

        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public List<Customer> getCustomerList(){
        Connection conn=null;
        try {
            List<Customer> list=new ArrayList<Customer>();
            String sql="select * from customer";
            conn= DriverManager.getConnection(url,userName,passWord);
            PreparedStatement stmt=conn.prepareStatement(sql);
            ResultSet rs=stmt.executeQuery();
            while (rs.next()){
                Customer customer=new Customer();
                customer.setId(rs.getInt("id"));
                customer.setName(rs.getString("name"));
                customer.setContact(rs.getString("contact"));
                customer.setEmail(rs.getString("email"));
                customer.setTelephone(rs.getString("telephone"));
                customer.setRemark(rs.getString("remark"));
                list.add(customer);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    public Customer getCustomer(int id){
        return null;
    }
    public boolean createCustomer(Map<String,Object> map){
        return false;
    }
    public boolean updateCustomer(int id,Map<String,Object> map){
        return false;
    }
    public boolean deleteCustomer(int id){
        return  false;

    }


}
